# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.adminRoleMenuDao import AdminRoleMenuDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertAdminRoleMenu', methods=['POST'])
def insert_admin_role_menuNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'roleId': request.json.get('roleId', ''),
        'roleTitle': request.json.get('roleTitle', ''),
        'actionKey': request.json.get('actionKey', '')
    }

    info = AdminRoleMenuDao().insertAdminRoleMenu(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findAdminRoleMenuById', methods=['POST'])
def find_admin_role_menu_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    roleMenuId = request.json.get('roleMenuId', '')
    info = AdminRoleMenuDao().findAdminRoleMenuById(roleMenuId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "No such data"}})


@app.route('/findAdminRoleMenuByCondition', methods=['POST'])
def find_admin_role_menu_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    info, total = AdminRoleMenuDao().findAdminRoleMenuByCondition(pageIndex=pageIndex, pageSize=pageSize,
                                                                  conditions=conditions, multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 0, "status": "No such data"}})


@app.route('/deleteAdminRoleMenuByIds', methods=['POST'])
def delete_admin_role_menu_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = AdminRoleMenuDao().deleteRoleMenu(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateAdminRoleMenuById', methods=['POST'])
def update_admin_role_menu_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        roleMenuId = request.json['roleMenuId']
    except:
        return jsonify({"message": {"code": 1, "status": "请传入要修改信息的ID"}})
    oldInfo = AdminRoleMenuDao().selectRoleMenu(roleMenuId)
    info = {
        'roleMenuId': request.json['roleMenuId'],
        'roleId': request.json.get('roleId', oldInfo[1]),
        'roleTitle': request.json.get('roleTitle', oldInfo[2]),
        'actionKey': request.json.get('actionKey', oldInfo[3])
    }

    info = AdminRoleMenuDao().updateRoleMenu(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/addRoleMenus', methods=['POST'])
def add_role_menusNoToken():
    """
    根据数组添加
    :return:
    """
    if not request.json:
        abort(400)
    try:
        addArr = request.json['addArr']
    except:
        return jsonify({"message": {"code": 0, "status": "传入数组addArr"}})

    info = AdminRoleMenuDao().add_role_menus(addArr)
    if info:
        return jsonify({"info": addArr, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
